# -*- coding: utf-8 -*-
# @Date    : 2016-08-16 19:45:06
# @Author  : fancy (fancy@thecover.cn)

import re

collection = [
    'django_migrations.py',
    'django_admin_log.py',
    'main_generation.py',
    'migrations.py',
    'api_user.doc',
    'user_group.doc',
    'accounts.txt',
]

def fuzzyfinder(user_input, collection):
    suggestion = []
    pattern = '.*?'.join(user_input)
    regex = re.compile(pattern)
    for item in collection:
        match = regex.search(item)
        if match:
            # import pdb; pdb.set_trace()
            suggestion.append((len(match.group()), match.start(), item))
    return [x for _, _, x in sorted(suggestion)]

def main():
    print fuzzyfinder('user', collection)

if __name__ == '__main__':
    main()
